<template>
  <el-row type="flex" justify="center">
    <el-form ref="form" :model="form" label-width="60px">
      <el-form-item>
        <div class="title">登录</div>
      </el-form-item>
      <!-- 这个是用户名输入框 -->
      <el-form-item label="账号">
        <el-input v-model="form.uname"></el-input>
      </el-form-item>
      <!-- 这个是密码输入框 -->
      <el-form-item label="密码">
        <el-input type="password" v-model="form.upwd"></el-input>
      </el-form-item>
      <el-form-item class="btn">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-row>
</template>

<script>
export default {
  data() {
    return {
      form: {
        uname: "",
        upwd: ""
      }
    };
  },
  methods: {
    onSubmit() {
      this.$axios({
        method: "POST",
        url: "/admin/account/login",
        data: this.form,
        // 由于在8080端口下去请求8899端口，涉及到跨域
        withCredentials: true
      }).then(res => {
        sessionStorage.setItem("userInfo", JSON.stringify(res.data.message))
        const { status, message } = res.data;
        if (status == 0) {
          // 登录成功
          this.$router.push("/admin");
        } else {
          // 登录失败
          this.$message.error(message);
        }
      });
    },
    handleReset() {
      this.form = {
        uname: "",
        upwd: ""
      };
    }
  }
};
</script>

<style>
.el-row {
  margin-top: 150px;
}
.title {
  text-align: center;
  margin-bottom: 20px;
  color: rgb(68, 160, 255);
}
.el-input {
  width: 350px;
}
.btn {
  text-align: center;
}
</style>
